//
//  ZYConfig.m
//  ZYhop
//
//  Created by Zyark on 2017/4/19.
//  Copyright © 2017年 Zyark. All rights reserved.
//

#import "ZYConfig.h"
#import "ZYUser.h"

NSString * const kLoginToken = @"loginToken";

NSString * const kUserID = @"userID";

NSString * const kUserName = @"userName";

NSString * const kNickname = @"nickname";

NSString * const kUserSex = @"userSex";

NSString *const kUserPhone = @"userPhone";

NSString *const kUserEmail = @"userEmail";

NSString * const kHeadImageURL = @"headImageURL";

NSString * const kIsNotFirstLoad = @"isNotFirstLoad";

NSString * const kHouseId = @"houseId";

/**是否开启手势密码*/
NSString * const kOnGesturePassword = @"onGesturePassword";

/**是否显示轨迹*/
NSString * const kOnDisplayTrack = @"onDisplayTrack";

/**是否在家*/
NSString * const kIsAtHome = @"isAtHome";

/**是否初始化语言*/
NSString * const kIsInitLanguage = @"isInitLanguage";

@implementation ZYConfig

#pragma mark - LoginToken
+ (void)clearLoginToken
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:kLoginToken];
}

+ (void)saveLoginToken:(NSString *)token{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:token forKey:kLoginToken];
    [defaults synchronize];
}

+(NSString *)loginToken{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:kLoginToken];
}

+(BOOL)isLogin{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *token =  [defaults objectForKey:kLoginToken];
    NSInteger userId = [defaults integerForKey:kUserID];
    return !NULLString(token) && userId != 0;
}

+ (void)saveCookies
{
    NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject: cookiesData forKey: @"sessionCookies"];
    [defaults synchronize];
}

#pragma mark - UserInfo

+ (void)saveUserInfo:(ZYUser *)user{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:user.userId forKey:kUserID];
    [defaults setObject:user.headImageUrl forKey:kHeadImageURL];
    [defaults setObject:user.username forKey:kUserName];
    [defaults setObject:user.nickname forKey:kNickname];
    if(!NULLString(user.token)){
        [defaults setObject:user.token forKey:kLoginToken];
    }
    [defaults setInteger:user.sex forKey:kUserSex];
    [defaults setObject:user.phone forKey:kUserPhone];
    [defaults setObject:user.email forKey:kUserEmail];
    [defaults synchronize];
}

+ (void)clearUserInfo{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:0 forKey:kUserID];
    [defaults setObject:@"" forKey:kHeadImageURL];
    [defaults setObject:@"" forKey:kUserName];
    [defaults setObject:@"" forKey:kLoginToken];
    [defaults setObject:@"" forKey:kUserPhone];
    [defaults setObject:@"" forKey:kUserSex];
    [defaults setObject:@"" forKey:kNickname];
    [defaults setObject:@"" forKey:kUserEmail];
    [defaults setObject:@"" forKey:kHouseId];
    [defaults setBool:NO forKey:kOnDisplayTrack];
    [defaults setBool:NO forKey:kOnGesturePassword];
    [defaults setObject:@"" forKey:@"gestureOneSaveKey"];//手势密码
    [defaults synchronize];
}

+(ZYUser *)userInfo{
    ZYUser *user = [ZYUser new];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [user setUserId:[defaults integerForKey:kUserID]];
    [user setHeadImageUrl:[defaults objectForKey:kHeadImageURL]];
    [user setUsername:[defaults objectForKey:kUserName]];
    [user setNickname:[defaults objectForKey:kNickname]];
    [user setToken:[defaults objectForKey:kLoginToken]];
    [user setSex:[defaults integerForKey:kUserSex]];
    [user setPhone:[defaults objectForKey:kUserPhone]];
    [user setEmail:[defaults objectForKey:kUserEmail]];
    return user;
}

+(NSString *)houseId{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:kHouseId];
}

+(void)setHouseId:(NSString *)houseId{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:houseId forKey:kHouseId];
}

+(NSInteger)userId{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults integerForKey:kUserID];
}

#pragma mark - FirstLoad
+ (BOOL)isFirstLoad{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return ![defaults boolForKey:kIsNotFirstLoad];
}

+ (void)setIsNotFirstLoad:(BOOL)isNotFirstLoad{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:isNotFirstLoad forKey:kIsNotFirstLoad];
}

#pragma mark - DisplayTrack
+(BOOL)isDisplayTrack{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:kOnDisplayTrack];
}

+ (void)setIsDisplayTrack:(BOOL)isDisplayTrack{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:isDisplayTrack forKey:kOnDisplayTrack];
}

#pragma mark - DisplayTrack
+(BOOL)isOnGesturePassword{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:kOnGesturePassword];
}

+ (void)setIsOnGesturePassword:(BOOL)isOnGesturePassword{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:isOnGesturePassword forKey:kOnGesturePassword];
}

#pragma mark - isAtHome
+(BOOL)isAtHome{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:kIsAtHome];
}

+ (void)setIsAtHome:(BOOL)isAtHome{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:isAtHome forKey:kIsAtHome];
}

#pragma mark - isInitLanguage
/**
 是否初始化语言
 */
+(BOOL)isInitLanguage{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:kIsInitLanguage];
}

/**
 设置是否初始化语言
 */
+ (void)setIsInitLanguage:(BOOL)isInitLanguage{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:isInitLanguage forKey:kIsInitLanguage];
}

@end
